#pragma once

#include <string>
#include <map>
#include <winrt/Microsoft.UI.Xaml.Controls.h>

namespace GMTK::Helpers
{
    inline void PopulateComboBoxFromMapStringKeys(
        winrt::Microsoft::UI::Xaml::Controls::ComboBox const& combo,
        const std::map<std::string, std::string>& keyValueMap,
        const std::string& selectedKey)
    {
        using namespace winrt;
        using namespace Microsoft::UI::Xaml::Controls;

        auto items = combo.Items();
        items.Clear();

        uint32_t index = 0;
        uint32_t selectedIndex = 0;

        for (const auto& pair : keyValueMap)
        {
            ComboBoxItem item;
            item.Content(box_value(to_hstring(pair.first)));
            items.Append(item);

            if (pair.first == selectedKey)
            {
                selectedIndex = index;
            }
            ++index;
        }

        const uint32_t size = items.Size();
        if (size == 0)
        {
            combo.SelectedIndex(-1);
            return;
        }
        if (selectedIndex >= size)
        {
            selectedIndex = 0;
        }
        combo.SelectedIndex(selectedIndex);
    }
}

